/*
 *  Author: Rico Yang
 *  gcc version: (Ubuntu 9.3.0-10ubuntu2)
 */

#include "seqListBasicOps.h"

int main(int argc, char const *argv[]) {
    SeqList seqList;
    initSeqList(&seqList);

    // select 记录用户输入选项
    // menuDisplayMode 用于调整菜单显示模式
    // 0 为友好模式, 显示人话, 默认模式
    // 1 为程序员模式, 显示函数名
    short select, menuDisplayMode = 0;
    // 显示菜单
    while (1) {
        printf("\n");
        if (menuDisplayMode == 0) {
            printf("******************************************\n");
            printf("*                                        *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*       ++    Welcome to use    ++       *\n");
            printf("*       ++   SeqList program!   ++       *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*                                        *\n");
            printf("*     [1] Display list.                  *\n");
            printf("*     [2] Clear list.                    *\n");
            printf("*     [3] Is the list empty?             *\n");
            printf("*     [4] Is the list full?              *\n");
            printf("*     [5] Insert at the end.             *\n");
            printf("*     [6] Insert at the beginning.       *\n");
            printf("*     [7] Insert by location.            *\n");
            printf("*     [8] Insert by array.               *\n");
            printf("*     [9] The lenth of the list.         *\n");
            printf("*     [10] Pop data at the end.          *\n");
            printf("*     [11] Pop data at the beginning.    *\n");
            printf("*     [12] Pop data by location.         *\n");
            printf("*     [13] Pop data by data.             *\n");
            printf("*     [14] Get location by data.         *\n");
            printf("*     [15] Get data by location.         *\n");
            printf("*     [16] Sort list.                    *\n");
            printf("*     [17] Reverse the list.             *\n");
            printf("*     [18] Destroy list.                 *\n");
            printf("*                                        *\n");
            printf("*     [-1] Menu display function name.   *\n");
            printf("*     [0] Quit program.                  *\n");
            printf("*                                        *\n");
            printf("******************************************\n");
        } else {
            printf("******************************************\n");
            printf("*                                        *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*       ++    Welcome to use    ++       *\n");
            printf("*       ++   SeqList program!   ++       *\n");
            printf("*       ++++++++++++++++++++++++++       *\n");
            printf("*                                        *\n");
            printf("*       [1] displaySeqList               *\n");
            printf("*       [2] clearSeqList                 *\n");
            printf("*       [3] isEmpty                      *\n");
            printf("*       [4] isFull                       *\n");
            printf("*       [5] insertAtTheEnd               *\n");
            printf("*       [6] insertAtTheBeginning         *\n");
            printf("*       [7] insertByLocation             *\n");
            printf("*       [8] insertByArray                *\n");
            printf("*       [9] getListLenth                 *\n");
            printf("*       [10] popAtTheEnd                 *\n");
            printf("*       [11] PopAtTheBeginning           *\n");
            printf("*       [12] PopByLocation               *\n");
            printf("*       [13] PopByData                   *\n");
            printf("*       [14] getLocationByData           *\n");
            printf("*       [15] getDataByLocation           *\n");
            printf("*       [16] sortList                    *\n");
            printf("*       [17] reverseList                 *\n");
            printf("*       [18] destroyList                 *\n");
            printf("*                                        *\n");
            printf("*       [-1] Menu display friendly.      *\n");
            printf("*       [0] Quit program.                *\n");
            printf("*                                        *\n");
            printf("******************************************\n");

        }

        printf("\nYou select: ");
        scanf("%hd", &select);
        if (select == 0) {
            exit(0);
        }
        printf("\x1b[H\x1b[2J");    // linux 终端清屏
        switch (select) {
            // case 加大括号更规范, 因为 case 只是标签
            // 如果在某个 case 下声明变量的话，作用域是在两个花括号之间
            // 也就是整个 switch 语句, 这样其他 case 也能看到，可能导致错误
            case 1: {
                displaySeqList(&seqList);
                break;
            }
            case 2: {
                clearSeqList(&seqList);
                break;
            }
            case 3: {
                isEmpty(&seqList);
                break;
            }
            case 4: {
                isFull(&seqList);
                break;
            }
            case 5: {
                DataType userData;
                printf("Input the data you want to insert: ");
                scanf("%d", &userData);
                eatline;
                if (insertAtTheEnd(&seqList, userData) == -1) {
                    printf("The list is full! Nothing changed.\n");
                } else {
                    printf("Successfully inserted.\n");
                }
                break;
            }
            case 6: {
                DataType userData;
                printf("Input the data you want to insert: ");
                scanf("%d", &userData);
                eatline;
                if (insertAtTheBeginning(&seqList, userData) == -1) {
                    printf("The list is full! Nothing changed.\n");
                } else {
                    printf("Successfully inserted data.\n");
                }
                break;
            }
            case 7: {
                DataType userData;
                int location;
                printf("Input the location you want to insert: ");
                scanf("%d", &location);
                eatline;
                printf("Input the data you want to insert: ");
                scanf("%d", &userData);
                eatline;
                if (insertByLocation(&seqList, location, userData) == -1) {
                    printf("The list is full! Nothing changed.\n");
                } else {
                    printf("Successfully inserted data.\n");
                }
                break;
            }
            case 8: {
                if (insertByArray(&seqList) == -1) {
                    printf("The list is full! Nothing changed.\n");
                } else {
                    printf("Successfully inserted data.\n");
                }
                break;
            }
            case 9: {
                getListLenth(&seqList);
                break;
            }
            case 10: {
                if (popAtTheEnd(&seqList) == -1) {
                    printf("There is nothing todo cause of empty list.\n");
                } else {
                    printf("Successfully poped data.\n");
                }
                break;
            }
            case 11: {
                if (popAtTheBeginning(&seqList) == -1) {
                    printf("There is nothing todo cause of empty list.\n");
                } else {
                    printf("Successfully poped data.\n");
                }
                break;
            }
            case 12: {
                int location;
                printf("Input the location you want to pop: ");
                scanf("%d", &location);
                short result = popByLocation(&seqList, location);
                if (result == -1) {
                    printf("Wrong operation!\n");
                } else if (result == -2) {
                    printf("Illegal location.\n");
                } else {
                    printf("Successfully poped data.\n");
                }
                break;
            }
            case 13: {
                DataType userData;
                printf("Input the data you want to pop: ");
                scanf("%d", &userData);
                popByData(&seqList, userData);
                break;
            }
            case 14: {
                DataType userData;
                int location;
                printf("Input the data you want to get location: ");
                scanf("%d", &userData);
                if ((location = getLocationByData(&seqList, userData)) == -1) {
                    printf("No result.\n");
                    break;
                }
                printf("Data %d is at %d.\n", userData, location);
                break;
            }
            case 15: {
                int location;
                DataType userData;
                printf("Input the locations you want to get data: ");
                scanf("%d", &location);
                if ((userData = getDataByLocation(&seqList, location)) == -1) {
                    printf("Illegal location.\n");
                    break;
                }
                printf("Data at %d is %d.\n", location, userData);
                break;
            }
            case 16: {
                sortList(&seqList);
                break;
            }
            case 17: {
                reverseList(&seqList);
                break;
            }
            case 18: {
                destroyList(&seqList);
                break;
            }
            case -1: {
                (menuDisplayMode == 0) ? (menuDisplayMode = 1) : (menuDisplayMode = 0);
                break;
            }
            default: {
                printf("Wrong selection. Please retry.\n");
                break;
            }
        }
    }
    // destroyList 按照程序逻辑不应由用户主动执行，而是在退出程序时自动执行
    // destroyList(&seqList);

    exit(0);
}
